
package writers;

import readers.*;
import containers.Groups;
import containers.TestScores;
import containers.TestsTaken;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.LinkedList;

/**
 * Handles the writing of information;
 * 
 * This assumes the information that is passed in is correct
 * The processors/handlers should control what is set here
 * @author Chris
 */
public class UserWriter {
    protected UserWriter() { }
    
    public static boolean setPassword(String userName, String pw) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation +  userName+"_passowrd.txt"));
            bw.write(pw);
            bw.newLine();
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    public static boolean setTeacher(String teacherName) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation +  teacherName+"_teacher.txt"));
            bw.newLine();
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    /*
     * 
     * 
     * The rest of the functions follow the same patterns
     * 
     *  Open reader
     *  write a blank character
     *  append all information in the linked list
     *  close the reader
     * 
     * 
     * 
     */
    public static boolean setStudentsGroups(String userName, LinkedList<Groups> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation +  userName+"_belongs.txt"));
            Groups grp;
            bw.write("");
            while(!lst.isEmpty()) {
                grp = lst.removeFirst();
                bw.append(grp.teacherName + " " + grp.groupName);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    

    public static boolean setTakenTests(String userName, LinkedList<TestsTaken> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation +  userName+"_taken.txt"));
            TestsTaken tt;
            bw.write("");
            while(!lst.isEmpty()) {
                tt = lst.removeFirst();
                bw.append(tt.teacher + " " + tt.group + " " + tt.test + " " + tt.score);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
        public static boolean appendTakenTests(String userName, String teacher, String grp, String test, String score) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation +  userName+"_taken.txt"));
            bw.append(teacher + " " + grp + " " + test + " " + score);
            bw.newLine();

            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
        
    public static boolean setTests(String teacherName, String groups, LinkedList<String> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_" + groups + "_tests.txt"));
            String test;
            bw.write("");
            while(!lst.isEmpty()) {
                test = lst.removeFirst();
                bw.append(test);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }

    public static boolean setTest(String teacherName, String groups,
                                  String test, LinkedList<String> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_" + groups + "_" + test + "_words.txt"));
            String _test;
            bw.write("");
            while(!lst.isEmpty()) {
                _test = lst.removeFirst();
                bw.append(_test);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }

    public static boolean setTestScores(String teacherName, String groups,
                                  String test, LinkedList<TestScores> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_" + groups + "_" + test + "_score.txt"));
            TestScores scores;
            bw.write("");
            while(!lst.isEmpty()) {
                scores = lst.removeFirst();
                bw.append(scores.userName + " " + scores.score);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //Writing this so that it is just quicker to write the other code
    public static boolean appendTestScores(String teacherName, String groups,
                                  String test, TestScores scores) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_" + groups + "_" + test + "_score.txt"));

            bw.append(scores.userName + " " + scores.score);
            bw.newLine();
   
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    public static boolean setTeacherGroups(String teacherName, LinkedList<String> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_teacher_groups.txt"));
            String groups;
            bw.write("");
            while(!lst.isEmpty()) {
                groups = lst.removeFirst();
                bw.append(groups);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    

    public static boolean setTeacherGroupAccess(String teacherName, String group
            , LinkedList<String> lst) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + teacherName + "_" + group + "_access_groups.txt"));
            String access;
            bw.write("");
            while(!lst.isEmpty()) {
                access = lst.removeFirst();
                bw.append(access);
                bw.newLine();
            }
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    public static boolean addUser(String userName, String password) {
        try {
            if(UserReader.fileExists(Reader.readLocation + userName+ "_password.txt")) {
                return false;
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(Reader.readLocation + userName+ "_password.txt"));
            bw.write(password);
            bw.newLine();
            bw.close();
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
}
